package com.wtwd.device.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.dao.UpgradeMapper;
import com.wtwd.device.model.entity.UpgradeFile;
import com.wtwd.device.model.entity.UpgradeStatement;
import com.wtwd.device.service.UpgradeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UpgradeServiceImpl implements UpgradeService {
    @Autowired
    private UpgradeMapper upgradeMapper;

    @Override
    public Integer addUpgradeInfo(UpgradeFile upgradeFile) {
        return upgradeMapper.addUpgradeInfo(upgradeFile);
    }

    @Override
    public Integer deleteUpgradeStatement(Integer id, String watchId) {
        return upgradeMapper.deleteUpgradeStatement(id, watchId);
    }


    @Override
    public Integer getUpgradeByWatchIdAndVersion(String watchId, String version) {
        return upgradeMapper.getUpgradeByWatchIdAndVersion(watchId, version);
    }

    @Override
    public List<UpgradeStatement> getUpgradeStatementByUpgradeId(Integer upgradeId) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getUpgradeStatementListByUpgradeId(Integer upgradeId) {
        return upgradeMapper.getUpgradeStatementListByUpgradeId(upgradeId);
    }

    @Override
    public List<Map<String, Object>> getLanguageList() {
        return upgradeMapper.getLanguageList();
    }

    @Override
    public Integer modifyUpgradeStatement(UpgradeStatement data) {
        return upgradeMapper.modifyUpgradeStatement(data);
    }

    @Override
    public Integer addUpgradeStatement(UpgradeStatement data) {
        return upgradeMapper.addUpgradeStatement(data);
    }

    @Override
    public List<String> getWatchIdList(String key, Long userId) {
        return upgradeMapper.getWatchIdList(key, userId);
    }

    @Override
    public Integer deleteUpgrade(Integer id) {
        upgradeMapper.deleteUpgradeStatementByUpgradeId(id);
        return upgradeMapper.deleteUpgrade(id);
    }

    @Override
    public List<UpgradeFile> getUpgradesByWatchId(String watchId) {
        if (StringUtils.isBlank(watchId)) {
            return null;
        }
        return upgradeMapper.getUpgradesByWatchId(watchId);
    }

    @Override
    public Map<String, Object> getUpgradeInfoList(Long userId, String watchId, Integer curPage, Integer pageSize) {
        //构建分页
        Page<Object> page = new Page<>(curPage, pageSize);
        IPage<UpgradeFile> iPage = upgradeMapper.getUpgradeInfoList(userId, watchId, page);
        List<UpgradeFile> records = iPage.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("total", iPage.getTotal());
        map.put("data", records);
        return map;
    }
}
